const nzhcn = require('nzh/cn'); //直接使用简体中文

/**
 * 将字符串中的<>&"特殊符号转成HTML表示法
 * @param sHtml 字符串
 * @returns 转换后的字符串
 */
export const encodeHtml = (sHtml) => {
    return sHtml.replace(/[<>&"]/g, function (c) {
        return { '<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;' }[c];
    });
};
/**
 * 获取url中对应参数的具体值
 * @param name 参数的key
 * @param str url
 * @returns key的对应值
 */
export const getUrlParam = (name, str) => {
    const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`);
    const r = str.substr(1).match(reg);
    if (r != null) return decodeURIComponent(r[2]);
    return null;
};
/**
 * 生成唯一的uuid
 * @param len uuid长度，默认为8
 * @param radix 进制数，默认为16进制
 * @returns uuid字符串
 */
export const getUuid = (len = 8, radix = 16) => {
    const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    const uuid = [];
    let i;
    radix = radix || chars.length;
    if (len) {
        for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
    } else {
        let r;
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';
        for (i = 0; i < 36; i++) {
            if (!uuid[i]) {
                r = 0 | (Math.random() * 16);
                uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
            }
        }
    }
    return uuid.join('');
};
/**
 * 根据文件名获取文件扩展名
 * @param data 文件全称
 * @returns 文件扩展名
 */
export const getExtension = (data: string) => {
    const extension = data.split('.')[1];
    return extension.toLocaleLowerCase();
};
/**
 * 获取文件名
 * @param name 文件全称
 * @returns 文件名
 */
export const getFileName = (name) => {
    return name.substring(0, name.lastIndexOf('.'));
};
/**
 * 数字金额转大写
 * @param numberValue 数字金额
 * @returns 大写的数字金额字符串
 */
export const bigMoney = (numberValue: number) => {
    const chineseValue = nzhcn.toMoney(numberValue, { outSymbol: false });
    return String(chineseValue).replace('元', '圆');
};
/**
 * 将url的参数转成对象
 * @param url url地址
 * @param symbol 以...作为分割符号，默认为'='
 * @returns
 */
export const urlParse = (url: string, symbol = '=') => {
    if (!url.split('?')[1]) {
        return;
    }
    const str = url.split('?')[1],
        items = str.split('&'),
        result = {};
    let res = {};
    for (let i = 0, len = items.length; i < len; ++i) {
        res = items[i].split(symbol);
        result[res[0]] = res[1];
    }
    return result;
};

/**
 * 数据脱敏
 * @param data 需要脱敏的字符串
 * @param start 从第几位开始
 * @param end 到第几位结束
 * @returns
 */
export const filterSensitiveData = (data: string, start: number, end: number) => {
    const dataViewerLength = start + end;
    if (dataViewerLength < data.length) {
        let char = '';
        for (let i = 0, len = data.length - dataViewerLength; i < len; i++) {
            char += '*';
        }
        return data.substring(0, start) + char + data.substring(data.length - end, data.length);
    } else {
        return data;
    }
};
